package com.mdtx.write;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.*;
import java.util.Map;

/**
 * 代码写入
 *
 * @author zhgz
 * @create 2017-07-26 22:32
 **/
@Component("generateCode")
public class GenerateCode {


    @Autowired
    Configuration config;

    public  void generate(Map<String, Object> data,String templateName,String filePath) throws TemplateException {

        try {
            Template template =config.getTemplate(templateName);
            OutputStream os = new FileOutputStream(new File(filePath));
            Writer w =  new OutputStreamWriter(os);
            template.process(data,w);
            os.flush();
            os.close();
        }catch (IOException e){
            e.printStackTrace();
        }

    }
}
